package com.example.ljw.frescowallpaper.view;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.example.ljw.frescowallpaper.R;
import com.example.ljw.frescowallpaper.adapter.MyFavoriteAdapter;
import com.example.ljw.frescowallpaper.base.BaseActivity;
import com.example.ljw.frescowallpaper.bean.UserBean;
import com.example.ljw.frescowallpaper.config.Constants;
import com.example.ljw.frescowallpaper.utils.PreUtils;
import com.example.ljw.frescowallpaper.widget.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

import static com.example.ljw.frescowallpaper.R.id.recyclerView_sortdetail;

public class MyFavoriteActivity extends BaseActivity {

    private Context mContext = this;
    private List<String> mList;
    private MyFavoriteAdapter myFavoriteAdapter;
    private RecyclerView recyclerView_sortdetail;
    private UserBean user;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_favorite);

//        initData();
//        initView("我的收藏");
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
        initView("我的收藏");
    }

    private void initData() {
        String objectId = PreUtils.readStrting(MyFavoriteActivity.this, Constants.KEY_OF_OBJECTID);
        user = new UserBean();
        BmobQuery<UserBean> query = new BmobQuery<>();
        query.getObject(objectId, new QueryListener<UserBean>() {
            @Override
            public void done(UserBean userBean, BmobException e) {
                mList = new ArrayList<String>();
                mList = userBean.getFavorite();
                mList.remove(0);
                initRecyclerView();
            }
        });
    }

    private void initRecyclerView() {
        //初始化recyclerView
        //设置recycleView
        recyclerView_sortdetail.setHasFixedSize(true);
        recyclerView_sortdetail.setItemAnimator(new DefaultItemAnimator());
        recyclerView_sortdetail.addItemDecoration(new DividerItemDecoration(mContext,
                DividerItemDecoration.VERTICAL_LIST));
        //设置布局管理器
        GridLayoutManager gridLayoutManager=new GridLayoutManager(mContext,2);
        recyclerView_sortdetail.setLayoutManager(gridLayoutManager);
        myFavoriteAdapter = new MyFavoriteAdapter(mContext,mList);
        recyclerView_sortdetail.setAdapter(myFavoriteAdapter);

    }
    private void initView(String name) {
        //设置toolbar支持actionbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setTitle(name);
        //recyclerView初始化
        recyclerView_sortdetail= (RecyclerView) findViewById(R.id.recyclerView_sortdetail);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
}
